package com.example.wangyeming.getuidemo.utils;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

import com.example.wangyeming.getuidemo.MainActivity;
import com.example.wangyeming.getuidemo.R;

/**
 * 发送自定义通知消息
 * Created by wangyeming on 15-6-10.
 */
public class MyNotification {

    /**
     * 自定义的通知栏样式*
     */
    public static final int NOTIFICATION_STYLE_1 = 1;
    public static final int NOTIFICATION_STYLE_2 = 2;
    public static final int NOTIFICATION_STYLE_3 = 3;
    private Context mContext;
    private NotificationManager mNotifyMgr;

    public MyNotification(Context context) {
        mNotifyMgr = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
        mContext = context;
    }

    /**
     * 设置通知栏样式
     * @param style
     * @param title
     * @param content
     * @return
     */
    public NotificationCompat.Builder styleNotification(int style, int number, String title, String content) {
        switch (style) {
            case NOTIFICATION_STYLE_1:
                return new NotificationCompat.Builder(mContext).setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle(title)
                        .setContentText(content)
                        .setAutoCancel(true)
                        .setTicker("测试通知来啦")
                        .setWhen(System.currentTimeMillis())
//                        .setPriority(Notification.PRIORITY_DEFAULT)
                        .setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)  //设置震动和铃声
                        .setSmallIcon(R.mipmap.cll_app_launcher)
                        .setNumber(number);
            case NOTIFICATION_STYLE_2:
                return new NotificationCompat.Builder(mContext).setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle(title)
                        .setContentText(content);
            case NOTIFICATION_STYLE_3:
                return new NotificationCompat.Builder(mContext).setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle(title)
                        .setContentText(content);
            default:
                return new NotificationCompat.Builder(mContext).setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle(title)
                        .setContentText(content);
        }
    }

    public PendingIntent getBroadcastPendingIntent(int requestCode) {
        Intent resultIntent = new Intent(mContext, MainActivity.class);
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        return PendingIntent.getBroadcast(
                mContext,
                requestCode,
                resultIntent,
                PendingIntent.FLAG_ONE_SHOT
        );
    }

    public PendingIntent getActivityPendingIntent(int requestCode) {
        Intent resultIntent = new Intent(mContext, MainActivity.class);
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        return PendingIntent.getActivity(
                mContext,
                requestCode,
                resultIntent,
                PendingIntent.FLAG_ONE_SHOT
        );
    }

    /**
     * 在广播中发送通知
     *
     * @param style   自定义样式
     * @param title
     * @param content
     */
    public void sendNotification(int notificationId, int requestCode,
                                 int style, String title, String content) {
        //获取该id的通知数
        int number = NotificationNumberUtil.getNotificationNumber(mContext, notificationId);
        //定义通知栏点击事件
        PendingIntent resultPendingIntent = getActivityPendingIntent(requestCode);
        //通知栏样式
        NotificationCompat.Builder builder = styleNotification(style, number, title, content);
        builder.setContentIntent(resultPendingIntent);
        builder.setAutoCancel(true);
        Notification notification = builder.build();
        //发送通知
        mNotifyMgr.notify(notificationId, notification);
    }

    public void removeNotification(int notificationId) {
        mNotifyMgr.cancel(notificationId);
    }

    public void removeAllNotification() {
        mNotifyMgr.cancelAll();
    }

}
